package com.gitee.sys.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mybase.top.entity.ShopInfo;
import com.gitee.sys.mapper.ShopInfoMapper;
import com.gitee.sys.service.IShopInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2018-11-17
 */
@Service
@Component
public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> implements IShopInfoService {

    @Override
    public IPage<ShopInfo> getPage(long current, long size, ShopInfo shopInfo) {
        IPage<ShopInfo> page = new Page<>(current, size);
        QueryWrapper<ShopInfo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(shopInfo.getShopCode())){
            queryWrapper.like("shop_code", shopInfo.getShopCode());
        }
        if(StringUtils.isNotEmpty(shopInfo.getShopName())){
            queryWrapper.like("shop_name", shopInfo.getShopName());
        }
        if(StringUtils.isNotEmpty(shopInfo.getShopShortName())){
            queryWrapper.like("shop_short_name", shopInfo.getShopShortName());
        }
        if(StringUtils.isNotEmpty(shopInfo.getRootIn())){
            queryWrapper.eq("root_in", shopInfo.getRootIn());
        }
        page = this.page(page, queryWrapper);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveShop(ShopInfo shopInfo) {
        shopInfo.setCreateTime(LocalDateTime.now());
        shopInfo.setIsLock(0);
        shopInfo.setIsValid(1);
        return baseMapper.insert(shopInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateShop(ShopInfo shopInfo) {
        shopInfo.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(shopInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delShop(String shopCode) {
        ShopInfo shopInfo = new ShopInfo();
        shopInfo.setIsValid(0);
        shopInfo.setShopCode(shopCode);
        UpdateWrapper<ShopInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_valid", 0);
        updateWrapper.set("update_time", LocalDateTime.now());
        updateWrapper.eq("shop_code", shopCode);
        return baseMapper.update(shopInfo, updateWrapper);
    }
}
